// //时间戳转换成日期时间
function js_date_time(unixtime) {
  unixtime = unixtime.replace(/-/g, '/').replace('T', ' ').substr(0, 19);
  var date = new Date(unixtime);
  date = new Date(date.getTime() - date.getTimezoneOffset() * 60000);
  var publishTime = Date.parse(new Date(date)) / 1000,
    date = new Date(publishTime * 1000),
    Y = date.getFullYear(),
    M = date.getMonth() + 1,
    D = date.getDate(),
    H = date.getHours(),
    m = date.getMinutes(),
    s = date.getSeconds();

  if (M < 10) {
    M = '0' + M;
  }
  if (D < 10) {
    D = '0' + D;
  }
  if (H < 10) {
    H = '0' + H;
  }
  if (m < 10) {
    m = '0' + m;
  }
  if (s < 10) {
    s = '0' + s;
  }
  var nowTime = new Date().getTime() / 1000,
    diffValue = nowTime - publishTime,
    diff_days = parseInt(diffValue / 86400),
    diff_hours = parseInt(diffValue / 3600),
    diff_minutes = parseInt(diffValue / 60),
    diff_secodes = parseInt(diffValue);
  if (diff_days > 0 && diff_days <= 2) {
    return diff_days + "天前";
  } else if (diff_days <= 0 && diff_hours > 0) {
    return diff_hours + "小时前";
  } else if (diff_hours <= 0 && diff_minutes > 0) {
    return diff_minutes + "分钟前";
  } else if (diff_secodes < 60) {
    if (diff_secodes <= 10) {
      return "刚刚";
    } else {
      // return diff_secodes + "秒前";
      return "几秒前";
    }
  } else if (diff_days >= 3 && diff_days < 30) {
    return M + '-' + D + ' ' + H + ':' + m;
  } else if (diff_days >= 30) {
    return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
  }
}

function trim(str){
  return str.replace(/^\s*|\s*$/g, '')
}

function formatDate(unixtime) {
  unixtime = unixtime.replace(/-/g, '/').replace('T', ' ').substr(0, 19);
  var date = new Date(unixtime);
  date = new Date(date.getTime() - date.getTimezoneOffset() * 60000);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = function () {
    return date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  }
 
  var minute = function () {
    return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  }
 
  var second = function () {
    return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  }
  return  month + "/" + day;
}


function get_time(unixtime) {
  unixtime = unixtime.replace(/-/g, '/').replace('T', ' ').substr(0, 19);
  var date = new Date(unixtime);
  date = new Date(date.getTime() - date.getTimezoneOffset() * 60000);
  var publishTime = Date.parse(new Date(date)) / 1000,
    date = new Date(publishTime * 1000),
    Y = date.getFullYear(),
    M = date.getMonth() + 1,
    D = date.getDate(),
    H = date.getHours(),
    m = date.getMinutes(),
    s = date.getSeconds();
    if (H < 10) {
      H = '0' + H;
    }
    if (m < 10) {
      m = '0' + m;
    }
    return H+":"+m
}
// 导出对外暴露的属性
module.exports = {
  js_date_time: js_date_time,
  formatDate:formatDate,
  get_time:get_time,
  trim: trim
}